//
//  markBtnView.m
//  WeShare2.0
//
//  Created by koki on 16/1/7.
//  Copyright © 2016年 WeShare. All rights reserved.
//

#import "MSMarkBtnView.h"

@interface MSMarkBtnView ()
{
    UITableView *m_TableView;
}
@end
@implementation MSMarkBtnView
@synthesize m_IndexPath;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self _initView];
    }
    
    return self;
}

-(void)setDataArray:(NSArray *)dataArray
{
    _dataArray=dataArray;
     [m_TableView reloadData];
}



- (void)_initView
{
    m_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.width)
                                               style:UITableViewStylePlain];
    m_TableView.delegate = self;
    m_TableView.dataSource = self;
    m_TableView.center = CGPointMake(self.frame.size.width * 0.5, self.frame.size.height * 0.5);
    m_TableView.showsVerticalScrollIndicator = NO;
    m_TableView.showsHorizontalScrollIndicator = NO;
    m_TableView.transform = CGAffineTransformMakeRotation(-M_PI / 2);
    m_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    m_TableView.backgroundView = nil;
    m_TableView.backgroundColor = [UIColor colorWithWhite:0.f alpha:0.4];
    [self addSubview:m_TableView];
    
    self.clipsToBounds=YES;
    m_IndexPath=[NSIndexPath indexPathForItem:0 inSection:0];
}

-(void)setM_IndexPath:(NSIndexPath *)indexPath
{
    m_IndexPath=indexPath;
    [m_TableView reloadData];
    [m_TableView scrollToRowAtIndexPath:m_IndexPath atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

-(void)markViewSelected:(NSString*)mark{}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.m_IndexPath=indexPath;
    if (self.delegate!=nil &&[self.delegate respondsToSelector:@selector(markViewSelected:)])
    {
        NSString* item=self.dataArray[indexPath.row];
        [self.delegate performSelector:@selector(markViewSelected:) withObject:item];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView* view=[[UIView alloc] init];
    view.backgroundColor=[UIColor clearColor];
    return view;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView* view=[[UIView alloc] init];
    view.backgroundColor=[UIColor clearColor];
    return view;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idty = @"imgshowCell";
    MSMarkCell *cell = [tableView dequeueReusableCellWithIdentifier:idty];
    if (nil == cell)
    {
        cell = [[MSMarkCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idty];
        cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);

    }
    cell.isSelected=(m_IndexPath.row==indexPath.row);
    NSString* str=self.dataArray[indexPath.row];
    cell.mark=str;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString* item=self.dataArray[indexPath.row];
    CGSize size=[HDUtil sizeWithString:item font:[UIFont fontWithName:@"STHeitiSC-Light" size:14] size:CGSizeMake(MAXFLOAT, MAXFLOAT)];
    return (size.width+20);
}

@end


@interface MSMarkCell ()
@property UILabel* markLabel;
@end
@implementation MSMarkCell

- (void)dealloc {
    self.mark = nil;
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self _initView];
    }
    return self;
}


- (void)_initView
{
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    self.backgroundColor=[UIColor clearColor];
    self.markLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    self.markLabel.textAlignment=NSTextAlignmentCenter;
    self.markLabel.textColor=[UIColor whiteColor];
    self.markLabel.backgroundColor=[UIColor clearColor];
    self.markLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:13];
    self.contentView.clipsToBounds=YES;
    [self.contentView addSubview:self.markLabel];
    self.clipsToBounds=NO;
    
}


-(void)setIsSelected:(BOOL)isSelected
{
    _isSelected=isSelected;
    if (isSelected)
    {
        self.contentView.backgroundColor=[UIColor colorWithWhite:1.f alpha:0.2f];
        self.markLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:15];
    }
    else
    {
        self.contentView.backgroundColor=[UIColor clearColor];
        self.markLabel.font=[UIFont fontWithName:@"STHeitiSC-Light" size:13];
    }
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    self.contentView.layer.cornerRadius=15;
    self.markLabel.frame=self.contentView.bounds;
}

-(void)setMark:(NSString *)mark
{
    _mark=mark;
    self.markLabel.text=mark;
}

@end






























